package net.onecloud.onecloud.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtils {
    private static SimpleDateFormat sf = null;
    public static String getCurrentDate2() {
        Date date = new Date();
        sf  = new SimpleDateFormat("yyyyMMddHHmmss");
        return sf.format(date);
    }

    /*获取系统时间 格式为 "yyyy-MM-dd*/
    public static String getCurrentDate1() {
        Date date = new Date();
        sf = new SimpleDateFormat("yyyyMMdd");
        return sf.format(date);
    }
    /*获取系统时间 格式为 "yyyy-MM-dd*/
    public static String getCurrentDate() {
        Date date = new Date();
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(date);
    }

    /*时间戳转换成字符窜*/
    public static String getDateToString(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        return sf.format(d);
    }

    /*将字符串转为时间戳*/
    public static long getStringToDate(long time) {
        sf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        Date date = new Date();
        try {
            date = sf.parse(time+"");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 直接获取时间戳
     *
     * @return
     */
    public static String getTimeStamp() {
        String currentDate = getCurrentDate();
        sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sf.parse(currentDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(date.getTime());
    }

    public static String getTimeStampBalance(long timestamp) {

        Date newDate = new Date();
        StringBuffer stringBuffer = new StringBuffer();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {

            Long diff = newDate.getTime() - timestamp;   //两时间差，精确到毫秒
            if (diff < 0) {
                return null;
            }
            Long day = diff / (1000 * 60 * 60 * 24);          //以天数为单位取整
            Long hour = (diff / (60 * 60 * 1000) - day * 24);             //以小时为单位取整
            Long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);    //以分钟为单位取整
            Long secone = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);




            if (day > 0) {
                stringBuffer.append(day + "天");
                return stringBuffer.toString();
            }
            if (hour > 0) {
                stringBuffer.append(hour + "小时");
                return stringBuffer.toString();
            }
            if (min > 0) {
                stringBuffer.append(min + "分");
                return stringBuffer.toString();
            }
            if (secone > 0) {
                stringBuffer.append(secone + "秒");
                return stringBuffer.toString();
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

}
